<template>
  <div class="musicPlayer" @touchmove.prevent>
    <div class="leave-button" @touchstart="send();sendInput()"></div>
    <div class="songData">
      <transition
        name="img-animate"
        enter-active-class="animate__animated animate__backInDown"
        leave-active-class="animate__animated animate__backOutUp"
        :duration="{ enter: 1200, leave: 600 }"
      >
        <div class="img-part" v-if="!lyricShow">
          <img :src="getsongdata.picUrl" alt="" />
        </div>
      </transition>

      <transition
        name="lyric-animate"
        enter-active-class="animate__animated animate__backInUp"
        leave-active-class="animate__animated animate__backOutDown"
        :duration="{ enter: 1200, leave: 600 }"
      >
        <div
          class="lyric"
          v-if="lyricShow"
          @touchstart="touchIn"
          @touchmove="touchOn"
          @touchend="touchUp"
          ref="lyricDiv"
        >
          <ul
            ref="lyricUl"
            :style="{ transform: `translateY(-${needmove}px)` , transition : transition}"
          >
            <li
              ref="lyricLi"
              :class="{ active: activeNum === index }"
              v-for="(item, index) in lyricsObjArr"
              :key="item.uid"
              @touchstart="changeTime"
              @touchend ="hoverTime(item.time)"
            >
              {{ item.lyric }}
            </li>
          </ul>
        </div>
      </transition>
    </div>

    <div class="song-text">
      <div class="left-part">
        <div class="song-title">{{ getsongdata.name }}</div>

        <div class="song-author">{{ getsongdata.artists }}</div>
      </div>

      <div class="right-part">
        <div class="right-button"></div>
      </div>
    </div>

    <div
      class="music-bar"
      ref="musicBar"
      @touchstart="changeProgress"
      @touchmove="changeProgress"
      @touchend="playMusic"
    >
      <div class="bar" :style="{ width: musicWidth * musicPercent + 'px' }">
        <div class="ball"></div>
      </div>
    </div>

    <div class="time-part">
      <div class="left-time">{{ newNowTime }}</div>

      <div class="right-time">{{ restTime }}</div>
    </div>

    <div class="play-button">
      <ul class="play-ul">
        <li class="iconfont icon-ziyuan50"></li>
        <li
          :class="{
            iconfont: true,
            'icon-bofang': isplay,
            'icon-bofang-bar': !isplay,
          }"
          @touchstart="playMusic"
        ></li>
        <li class="iconfont icon-ziyuan49"></li>
      </ul>
    </div>

    <div class="voice-part">
      <div class="iconfont 24gl-volumeLow icon-24gl-volumeLow"></div>

      <div
        class="bar-part"
        ref="voiceBar"
        @touchstart="changeAmount"
        @touchmove="changeAmount"
      >
        <div class="bar" :style="{ width: voiceWidth * voicenum + 'px' }">
          <div class="ball"></div>
        </div>
      </div>

      <div class="iconfont 24gl-volumeLow icon-24gl-volumeHigh"></div>
    </div>

    <div class="bottom-button">
      <ul class="button-part">
        <li class="iconfont icon-geci" @touchstart="lyric"></li>
        <li class="iconfont icon-lanya"></li>
        <li class="iconfont icon-liebiao"></li>
      </ul>
    </div>
    <div class="bg"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false,
      nowPlay: true,
      voiceWidth: 0,
      musicWidth: 0,
      musicLeft: 0,
      voiceLeft: 0,
      lyricShow: false,
      lyricsObjArr: [],
      ulHight: 0,
      liHight: 0,
      divHight: 0,
      touchFirst: 0,
      touchMove: 0,
      isTouch : false,
      transition : "transform 0.2s",
      touchTime : 0,
      clickTime : 0,
      setTime : null,
      changeinput : false,
      inputTime : null
    };
  },
  props: {
    getsongdata: {
      type: Object,
    },
    nowtime: {
      type: Number,
    },
    maxtime: {
      type: Number,
    },
    isplay: {
      type: Boolean,
    },
    voicenum: {
      type: Number,
    },
    songid: {
      type: Number,
    },
  },
  computed: {
    musicPercent() {
      let percent = 0;
      percent = this.nowtime / this.maxtime;
      return percent;
    },
    voicePercent() {
      let percent = 0;
      return percent;
    },
    newNowTime() {
      let str = this.getTime(this.nowtime);
      return str;
    },
    restTime() {
      let str = this.getTime(this.maxtime - this.nowtime);
      return str;
    },
    activeNum() {
      let i = -1 
      if (this.lyricsObjArr.length === 0) {
        return;
      } else {
        this.lyricsObjArr.forEach((item) => {
          if (this.nowtime > item.time) {
            i ++
          }
        });
      }
      return i;
    },
    needmove(){
      let i = 0;
      if(this.isTouch){
        i = this.touchMove;
      }else {
        i = this.activeNum * this.liHight - (this.divHight/2)
        this.touchMove = i;
      }
      return i;
    }
  },
  methods: {
    send() {
      this.$emit("getshow", this.isShow);
    },
    playMusic() {
      this.$parent.musicPlay();
    },
    changeAmount(e) {
      let x = e.changedTouches[0].pageX - this.voiceLeft;
      x = x < 0 ? 0 : x;
      x = x > this.voiceWidth ? this.voiceWidth : x;
      let percent = (x / this.voiceWidth).toFixed(2);
      this.$parent.amount(percent);
    },
    changeProgress(e) {
      let x = e.changedTouches[0].pageX - this.musicLeft;
      x = x < 0 ? 0 : x;
      x = x > this.musicWidth ? this.musicWidth : x;
      let percent = (x / this.musicWidth).toFixed(2);
      this.$parent.progress(percent);
    },
    getTime(time) {
      const h = parseInt(time / 3600);
      const minute = parseInt((time / 60) % 60);
      const second = Math.floor(time % 60);

      const hours = h < 10 ? "0" + h : h;
      const formatSecond = second > 59 ? 59 : second;
      return `${minute < 10 ? "0" + minute : minute}:${
        formatSecond < 10 ? "0" + formatSecond : formatSecond
      }`;
    },
    lyric() {
      this.lyricShow = !this.lyricShow;
    },
    formatLyricTime(time) {
      // 格式化歌词的时间 转换成 sss:ms
      const regMin = /.*:/;
      const regSec = /:.*\./;
      const regMs = /\./;

      const min = parseInt(time.match(regMin)[0].slice(0, 2));
      let sec = parseInt(time.match(regSec)[0].slice(1, 3));
      const ms = time.slice(
        time.match(regMs).index + 1,
        time.match(regMs).index + 3
      );
      if (min !== 0) {
        sec += min * 60;
      }
      return Number(sec + "." + ms);
    },
    touchIn(e) {
      clearTimeout(this.setTime);
      this.isTouch = true;
      this.touchFirst = e.changedTouches[0].clientY + this.touchMove;
      this.transition = "none"
    },
    touchOn(e) {
      this.touchMove = this.touchFirst - e.changedTouches[0].clientY;
      if (this.touchMove < 0) {
        this.touchMove = 0;
        return;
      } else if (this.touchMove > this.ulHight - this.divHight / 2) {
        this.touchMove = this.ulHight - this.divHight / 2;
        return;
      }
    },
    touchUp() {
      this.setTime = setTimeout(()=>{
        this.isTouch = false;
      },2000)
      this.transition = "transform 0.2s";
    },
    changeTime(){
        this.touchTime = new Date();
    },
    hoverTime(num){
      this.clickTime = new Date()
      if((this.clickTime-this.touchTime)>700){
        let percent = (num / this.maxtime).toFixed(2);
        this.$parent.progress(percent);
        this.$parent.musicPlay();
      };
    },
    sendInput(){
       this.$emit("haveinput",{isFasle : this.changeinput,isTrue : true});
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.musicWidth = this.$refs.musicBar.offsetWidth;
      this.voiceWidth = this.$refs.voiceBar.offsetWidth;
      this.voiceLeft = this.$refs.voiceBar.offsetLeft;
      this.musicLeft = this.$refs.musicBar.offsetLeft;
    });
    if (this.songid === undefined) {
      return;
    } else {
      axios
        .get(`/api/lyric?id=${this.songid}`)
        .then((response) => {
          let lyric = response.data.lrc.lyric;
          let regTime = new RegExp(/\[\d{2}:\d{2}.\d{2,3}\]/);
          const regNewLine = /\n/;
          const lineArr = lyric.split(regNewLine);
          lineArr.forEach((item) => {
            if (item === "") return;
            const obj = {};
            const time = item.match(regTime);

            obj.lyric =
              item.split("]")[1].trim() === "" ? "" : item.split("]")[1].trim();
            obj.time = time
              ? this.formatLyricTime(time[0].slice(1, time[0].length - 1))
              : 0;
            obj.uid = Math.random().toString().slice(-6);
            if (obj.lyric === "") {
            } else {
              this.lyricsObjArr.push(obj);
            }
          });
        });
    }
  },
  updated() {
    this.$nextTick(() => {
      let ulrefs = this.$refs.lyricUl;
      let lirefs = this.$refs.lyricLi;
      let divrefs = this.$refs.lyricDiv;
      if (
        ulrefs === undefined ||
        lirefs === undefined ||
        divrefs === undefined
      ) {
        return;
      } else {
        this.ulHight = ulrefs.offsetHeight;
        this.liHight = lirefs[0].offsetHeight;
        this.divHight = divrefs.offsetHeight;
      }
    });
  },
};
</script>

<style lang="scss" scoped>
.musicPlayer {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0rem;
  left: 0rem;
  z-index: 1000;
  overflow: hidden;
  background: #fff;
  user-select: none;
}

.leave-button {
  width: 2.5rem;
  height: 1%;
  background: rgba(0, 0, 0, 0.5);
  margin: 1rem auto;
  border-radius: 0.5rem;
}

.songData {
  width: 90%;
  margin: auto;
  height: 43%;
}

.lyric {
  width: 100%;
  height: 100%;
  overflow: hidden;

  > ul {
    width: 90%;
    margin: auto;

    > li {
      padding: 0.5rem 0.5rem;
      transition: all 0.25s;
      color: rgba(54, 54, 54, 0.5);
      font-size: 1rem;
    }

    .active {
      background: #33ccba52;
      transform: scale(1.1);
      font-weight: bolder;
    }
  }
}

.img-part {
  width: 80%;
  border-radius: 0.5rem;
  overflow: hidden;
  margin: auto;

  > img {
    width: 100%;
    height: auto;
  }
}

.song-text {
  width: 90%;
  margin: 1rem auto;
  display: flex;

  .left-part {
    flex: 1;
  }

  .song-title {
    width: 15rem;
    height: 1.5rem;
    font-size: 1.4rem;
    line-height: 1.5rem;
    font-weight: bold;
    color: #fff;
    overflow:hidden; //超出的文本隐藏
    text-overflow:ellipsis; //溢出用省略号显示
    white-space:nowrap; //溢出不换行
  }

  .song-author {
    width: 100%;
    height: 1.5rem;
    font-size: 1.4rem;
    line-height: 1.5rem;
    color: rgba(255, 255, 255, 0.5);
  }

  .right-part {
    width: 2rem;
    height: 3rem;
    position: relative;

    .right-button {
      width: 2rem;
      height: 2rem;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.5);
      position: absolute;
      top: 50%;
      transform: translateY(-50%);

      &::after {
        content: "...";
        color: #fff;
        font-size: 1.7rem;
        position: absolute;
        top: 28%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }
  }
}

.music-bar {
  width: 90%;
  margin: 1rem auto;
  background: rgba(255, 255, 255, 0.5);
  height: 0.3rem;
  border-radius: 8rem;

  .bar {
    width: 0;
    height: 100%;
    position: relative;
    background: #fff;

    .ball {
      width: 0.5rem;
      height: 0.5rem;
      position: absolute;
      right: 0px;
      transform: translate(25%, -25%);
      background: white;
      border: 1px solid #ccc;
      box-sizing: border-box;
      border-radius: 50%;
    }
  }
}

.time-part {
  width: 90%;
  margin: auto;
  display: flex;
  height: 2%;

  .left-time {
    flex: 1;
    font-size: 0.5rem;
    color: rgba(255, 255, 255, 0.8);
  }

  .right-time {
    flex: 1;
    font-size: 0.5rem;
    color: rgba(255, 255, 255, 0.8);
    text-align: right;
  }
}

.play-button {
  width: 75%;
  margin: auto;
  height: 10%;
  padding: 1rem 0rem;

  .play-ul {
    width: 100%;
    display: flex;
    height: 100%;

    > li {
      flex: 1;
      height: 100%;
      text-align: center;
      color: #fff;
      position: relative;
    }

    .iconfont {
      font-size: 2rem;
    }

    .icon-bofang-bar {
      font-size: 4rem;
      position: relative;

      &::before {
        position: absolute;
        left: 50%;
        transform: translate(-50%, -22.5%);
      }
    }
  }
}

.voice-part {
  width: 90%;
  margin: 0.8rem auto;
  height: 0.3rem;
  display: flex;
  .iconfont {
    flex: 1;
    height: 0.3rem;
    color: #fff;
    text-align: center;
    line-height: 0.3rem;
  }

  .bar-part {
    width: 80%;
    background: rgba(255, 255, 255, 0.5);
    height: 0.3rem;
    border-radius: 8rem;

    .bar {
      width: 0;
      height: 100%;
      position: relative;
      background: #fff;

      .ball {
        width: 1rem;
        height: 1rem;
        position: absolute;
        right: 0px;
        transform: translate(30%, -30%);
        background: white;
        border: 1px solid #ccc;
        box-sizing: border-box;
        border-radius: 50%;
      }
    }
  }
}
.bottom-button {
  width: 75%;
  margin: auto;
  height: 10%;
  padding: 1rem 0rem;

  .button-part {
    width: 100%;
    display: flex;
    height: 100%;

    > li {
      flex: 1;
      height: 100%;
      text-align: center;
      color: #fff;
    }

    .iconfont {
      font-size: 2rem;
    }
  }
}
.bg {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0px;
  left: 0px;
  z-index: -1;
  background-image: linear-gradient(0deg, #33CCBB, #fff);
  filter:blur(5px);
}
</style>